<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="../__/import.js"></script>
<title>登录</title>
<style type="text/css">
.form-wrap {
	position: fixed;
	top: 50%;
	left: 50%;
	margin-top: -180px;
	margin-left: -225px;
	width: 400px;
	padding: 25px 50px 5px 5px;
	background: #fefefe;
	transition: .2s;
	border-radius: 10px;
	border: 1px solid #efefef;
	box-shadow: 0 0 5px rgba(0, 0, 0, .1);
}

.from-title {
	border: none;
	font-size: 20px;
	padding-left: 35px;
}
</style>
<script>
	if (window.location.href != window.top.location.href) {
		var allowEmbedded = __.decodeUrlParams()['allowEmbedded'];
		if (allowEmbedded !== false) {
			try {
				top.location.href = __.url('/view/login.html');
			} catch (e) {
			}
		}
	}
</script>
</head>
<body>
	<div id="vm" v-cloak>
		<div class="form-wrap">
			<form is="el-form" label-width="80px">
				<fieldset class="from-title">
					<legend>系统登录</legend>
				</fieldset>
				<div is="el-form-item" label="账号">
					<input type="text" is="el-input" ref="username" @keyup.enter.native="enter(0)" v-model.trim="token.username" />
				</div>
				<div is="el-form-item" label="密码">
					<input is="el-input" type="password" ref="password" @keyup.enter.native="enter(1)" v-model="token.password" auto-complete="off" show-password/>
				</div>
				<div is="el-form-item">
					<button is="el-button" type="primary" @click="submit()">登录</button>
					<button is="el-button" @click="reset()">重置</button>
				</div>
			</form>
		</div>
	</div>
	<script>
		new Vue({
			el : '#vm',
			data : {
				token : {
					username : '',
					password : ''
				}
			},
			methods : {
				enter : function(index) {
					var vm = this;
					vm.token.username = __.trim(vm.token.username);
					if (index == 0) {
						vm.$refs.password.focus();
					} else if (index == 1 && __.isEmpty(vm.token.username)) {
						vm.$refs.username.focus();
					} else {
						vm.submit();
					}
				},
				submit : function() {
					var vm = this;
					var token = vm.token;
					token.username = __.trim(token.username);
					if (__.isEmpty(token.username)) {
						vm.$message({
							message : '请输入账号',
							type : 'warning'
						});
						return;
					}
					if (__.isEmpty(token.password)) {
						vm.$message({
							message : '请输入密码',
							type : 'warning'
						});
						return;
					}
					__.mask();
					__.api('POST', '/rest/auth/getPublicKey', {}, function(result) {
						if (__.err(result)) {
							__.unmask();
							__.message({
								message : result.message || '秘钥获取失败',
								type : 'warning'
							});
							return;
						}
						var rsaKey = result.data;
						var encrypt = new JSEncrypt();
						encrypt.setPublicKey(rsaKey.publicKey);
						var params = {};
						params.username = token.username;
						params.password = encrypt.encrypt(token.password);
						params.rsaId = rsaKey.rsaId;
						__.api('POST', '/rest/auth/login', params, function(result) {
							__.unmask();
							if (__.err(result)) {
								__.message({
									message : result.message || '登录失败',
									type : 'warning'
								});
							} else {
								__.setAccessToken(result.data.value);
								__.navigateTo('/view/home.html');
							}
						});
					});
				},
				reset : function() {
					var vm = this;
					vm.token = {
						username : '',
						password : ''
					};
				}
			}
		});
	</script>
</body>
</html>